package com.xiaoge.biz.controller;


import com.xiaoge.auth.annotation.ControllerEndpoint;
import com.xiaoge.biz.entity.ProductCategory;
import com.xiaoge.biz.service.ProductCategoryService;
import com.xiaoge.response.Result;
import com.xiaoge.system.entity.Menu;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xiaoge
 * @since 2021-03-21
 */
@RestController
@CrossOrigin
@RequestMapping("/productCategory")
public class ProductCategoryController {
    @Autowired
    private ProductCategoryService productCategoryService;

    /**
     * 查询所有物资类别列表
     *
     * @return
     */
    @ApiOperation(value = "查询所有物资类别列表")
    @GetMapping("/getList")
    public Result getCategory() throws CloneNotSupportedException {
        List<ProductCategory> productCategories = productCategoryService.categoryTreeBuild(productCategoryService.getAll());
        return Result.ok().data("menus", productCategories);
    }

    /**
     * 添加物资分类
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "添加物资分类失败", operation = "添加物资分类")
    @ApiOperation(value = "添加物资分类")
    @PostMapping("/addCategory")
    public Result addCategory(@RequestBody ProductCategory category) {
        productCategoryService.add(category);
        return Result.ok();
    }

    /**
     * 修改物资分类
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "修改物资分类失败", operation = "修改物资分类")
    @ApiOperation(value = "修改物资分类")
    @PutMapping("/updateCategory")
    public Result updateCategory(@RequestBody ProductCategory category) {
        productCategoryService.updateMenu(category);
        return Result.ok();
    }

    /**
     * 删除物资分类
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "删除物资分类失败", operation = "删除物资分类")
    @ApiOperation(value = "删除物资分类")
    @DeleteMapping("/delCategory")
    public Result delCategory(Long id) {
        productCategoryService.delMenu(id);
        return Result.ok();
    }
}
